package com.hemaapp.yjnh.activity;

import java.util.ArrayList;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.util.XtomWindowSize;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.CmnProductionAdapter;
import com.hemaapp.yjnh.bean.AdImage;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.listener.OnBlogItemClickListener;
import com.hemaapp.yjnh.view.ImageCarouselBanner;

/**地标
 * @author GuiyouZhang
 * Date：2015年12月18日 下午5:06:50
 * Email:guiyouzhang@163.com
 */
public class LandMarkActivity extends BaseActivity implements OnClickListener{

	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;
	private ImageView titleSearch;
	private XtomRefreshLoadmoreLayout layoutRefresh;
	private XtomListView listview;
	private LinearLayout ll_return_top;
	
	private View headerView;
	private ImageCarouselBanner imageCarouselBanner;
	private TextView tvNortheast;
	private TextView tvEast;
	private TextView tvSouth;
	private TextView tvNorth;
	private TextView tvNorthwest;
	private TextView tvSouthwest;
	private TextView tvCentral;

	private User user;
	private int page;
	private ArrayList<AdImage> imgs = new ArrayList<>();
	private ArrayList<Blog> mBlogs = new ArrayList<>();//推荐的地标商品
	private CmnProductionAdapter mAdapter;//
	private String area = "无";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_organic);
		user = getApplicationContext().getUser();
		super.onCreate(savedInstanceState);
		init();
	}
	
	private void init() {
		headerView = LayoutInflater.from(mContext).inflate(R.layout.header_landmark, null);
		findHeaderViews();
		listview.addHeaderView(headerView);
		getImageList();
	}
	/**获取图片列表
	 * 
	 */
	private void getImageList() {
        getNetWorker().ModelADList("4", "");
	}
	
	/**获取商品列表
	 * 
	 */
	private void getGoods() {
		getNetWorker().blogList("4", area, "1", "1", "", "", "1", "", "", "", "", "", "", "", page+"");
	}
	
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case MODELAD_LIST:
		case BLOG_LIST:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case MODELAD_LIST:
		case BLOG_LIST:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case MODELAD_LIST:
			@SuppressWarnings("unchecked")
			HemaPageArrayResult<AdImage> aResult = (HemaPageArrayResult<AdImage>) baseResult;
			if (aResult == null || aResult.getObjects() == null)
				return;
			if (imgs == null) {
				imgs = new ArrayList<AdImage>();
			} else {
				imgs.clear();
			}
			imgs.addAll(aResult.getObjects());
			initPager();
			getGoods();
			break;
		case BLOG_LIST:
			HemaPageArrayResult<Blog> bResult = (HemaPageArrayResult<Blog>) baseResult;
			if (bResult == null || bResult.getObjects() == null)
				return;
			if (page == 0) {// 刷新
				layoutRefresh.refreshSuccess();
				mBlogs.clear();
				this.mBlogs.addAll(bResult.getObjects());
			}else {
				layoutRefresh.loadmoreSuccess();
				if (bResult.getObjects().size()>0) {
					this.mBlogs.addAll(bResult.getObjects());
				}else {
					layoutRefresh.setLoadmoreable(false);
					XtomToastUtil.showShortToast(mContext, "已经到最后啦");
				}
			}
			refreshDatas(-1);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case MODELAD_LIST:
		case BLOG_LIST:
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
			}else {
				layoutRefresh.loadmoreFailed();
			}
			XtomToastUtil.showShortToast(mContext, "获取商品信息失败，"+baseResult.getMsg());
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case MODELAD_LIST:
		case BLOG_LIST:
			if (page == 0) {// 刷新
				layoutRefresh.refreshFailed();
			}else {
				layoutRefresh.loadmoreFailed();
			}
			XtomToastUtil.showShortToast(mContext, "获取商品信息失败，请稍后再试");
			break;
		default:
			break;
		}
	}

	/**商品列表
	 * @param failedType
	 */
	private void refreshDatas(int failedType) {
		if (mAdapter == null) {
			mAdapter = new CmnProductionAdapter(mContext, mBlogs);
			mAdapter.setItemClickListener(new OnBlogItemClickListener() {
				@Override
				public void onClickListener(int position) {
                    Intent it = new Intent(mContext, CmnProductionDetailActivity.class);
                    it.putExtra("blog_id", mBlogs.get(position).getId());
                    mContext.startActivity(it);
				}

				@Override
				public void onCartListener(int position) {

				}
			});
			mAdapter.setEmptyString("暂无商品");
			listview.setAdapter(mAdapter);
		}else {
			mAdapter.notifyDataSetChanged();
		}
		mAdapter.setFailtype(failedType);
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);
		titleSearch = (ImageView) findViewById(R.id.title_right_img);

		layoutRefresh = (XtomRefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		listview = (XtomListView)findViewById( R.id.listview );
		ll_return_top = (LinearLayout) findViewById(R.id.ll_return_top);//回到顶部
	}

	private void findHeaderViews() {
		imageCarouselBanner = (ImageCarouselBanner) headerView.findViewById(R.id.image_carousel_banner);
		tvNortheast = (TextView) headerView.findViewById( R.id.tv_northeast );
		tvEast = (TextView) headerView.findViewById( R.id.tv_east );
		tvSouth = (TextView) headerView.findViewById( R.id.tv_south );
		tvNorth = (TextView) headerView.findViewById( R.id.tv_north );
		tvNorthwest = (TextView) headerView.findViewById( R.id.tv_northwest );
		tvSouthwest = (TextView) headerView.findViewById( R.id.tv_southwest );
		tvCentral = (TextView) headerView.findViewById( R.id.tv_central );
		
		tvNortheast.setOnClickListener(this);
		tvEast.setOnClickListener(this);
		tvSouth.setOnClickListener(this);
		tvNorth.setOnClickListener(this);
		tvNorthwest.setOnClickListener(this);
		tvSouthwest.setOnClickListener(this);
		tvCentral.setOnClickListener(this);
	}
	
	@Override
	protected void getExras() {

	}

	@Override
	protected void setListener() {
		titleLeft.setOnClickListener( this );
		titleText.setText("地标");
		titleRight.setVisibility(View.GONE);
		titleSearch.setVisibility(View.VISIBLE);
		titleSearch.setOnClickListener(this);
		layoutRefresh.setOnStartListener(new StartListener());
		ll_return_top.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			finish();
			break;
		case R.id.title_right_img://进入搜索界面
			Intent itSearch = new Intent(mContext, SearchActivity.class);
			itSearch.putExtra("type", 4);
			itSearch.putExtra("assort", "地标");
			startActivity(itSearch);
			break;
		case R.id.ll_return_top:
			listview.setSelection(0);
			break;
		case R.id.tv_northeast:
			area = "东北";
            goGoodsList();
			break;
		case R.id.tv_east:
			area = "华东";
            goGoodsList();
			break;
		case R.id.tv_south:
			area = "华南";
            goGoodsList();
			break;
		case R.id.tv_north:
			area = "华北";
            goGoodsList();
			break;
		case R.id.tv_northwest:
			area = "西北";
            goGoodsList();
			break;
		case R.id.tv_southwest:
			area = "西南";
            goGoodsList();
			break;
		case R.id.tv_central:
			area = "华中";
			goGoodsList();
			break;
		default:
			break;
		}
	}
	
	/**进入商品列表界面
	 * 
	 */
	private void goGoodsList() {
		Intent it = new Intent(mContext, CmnProductionListWithDistrictActivity.class);
		it.putExtra("keytype", "4");
		it.putExtra("keyid", area);
		it.putExtra("keyword", "");
		it.putExtra("saleflag", "");
		it.putExtra("topflag", "");
		it.putExtra("cheapflag", "");
		it.putExtra("score_percent", "");
		it.putExtra("seller_id", "");
		it.putExtra("district_name", "");
		mContext.startActivity(it);
	}
	
	private class StartListener implements OnStartListener {

		@Override
		public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
		    page = 0;
		    getImageList();
		}

		@Override
		public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
			page++;
			getGoods();
		}
	}	
	
	private void initPager() {
		ArrayList<String> urls = new ArrayList<>();
		for (AdImage item : imgs) {
			urls.add(item.getImgurl());
		}
		imageCarouselBanner.getLayoutParams().height = XtomWindowSize.getWidth(mContext)/2;
		imageCarouselBanner.onInstance(mContext, urls, R.drawable.ad_show_indicator,
				new ImageCarouselHeadClickListener(mContext, imgs, "2"));
	} 
}
